package cn.zh.stan.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.zcw.togglebutton.ToggleButton;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.ReItem;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.citythreelist.ProvinceActivity;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class AddAdress extends BaseActivity {

    ImageView adr_back;

     LinearLayout choarea;
     TextView chdress;


    MyApplication mapp;
    ToggleButton toggle;

    TextView submit;

    String type,uid,companyName,personName,dtaddress,moblie,isDefault;
    String provinceId="",cityId="",countyId="",provinceName="",cityName="",countyName="";
    EditText pename,adphone,comname,detailtxt;

   ReItem reItem=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_adress);
        mapp= (MyApplication) getApplication();
        type=getIntent().getStringExtra("type");
        if (type.equals("1")){
            reItem= (ReItem) getIntent().getSerializableExtra("Item");
            }

        uid=mapp.getSp().getString("uid","");
        init();
       }


    public void init(){
        adr_back= (ImageView) findViewById(R.id.adr_back);
        choarea= (LinearLayout) findViewById(R.id.chadres);
        chdress= (TextView) findViewById(R.id.chdress);
        toggle= (ToggleButton) findViewById(R.id.toggle);
        submit= (TextView) findViewById(R.id.submit);
        pename= (EditText) findViewById(R.id.pename);
        adphone= (EditText) findViewById(R.id.adphone);
        comname= (EditText) findViewById(R.id.comname);
        detailtxt= (EditText) findViewById(R.id.detailtxt);

        adr_back.setOnClickListener(this);
        choarea.setOnClickListener(this);
        submit.setOnClickListener(this);
        toggle.setOnToggleChanged(new ToggleButton.OnToggleChanged() {
            @Override
            public void onToggle(boolean on) {
                if (on){
                    isDefault="1";
                }else {
                    isDefault="0" ;
                }
            }
        });
        if (null!=reItem){
            pename.setText(reItem.getPersonName());
            adphone.setText(reItem.getMoblie());
            comname.setText(reItem.getCompanyName());
            chdress.setText(reItem.getProvinceName()+"-"+reItem.getCityName()+"-"+reItem.getCountyName());
            detailtxt.setText(reItem.getAddress());
            if (reItem.getIsDefault().equals("1")){
                toggle.setToggleOn();
            }else {
                toggle.setToggleOff();
             }
            }
          }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapp.isIscity()){
            provinceName=mapp.getProviece().getName();
            cityName=mapp.getCityitem().getName();
            countyName=mapp.getAreaim().getName();
            provinceId=mapp.getProviece().getId();
            cityId=mapp.getCityitem().getId();
            countyId=mapp.getAreaim().getId();
            chdress.setText(provinceName+"-"+cityName+"-"+countyName);
            mapp.setIscity(false);
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.adr_back:
                finish();
                break;
            case R.id.chadres:
                mapp.setArechose(1);
                //选择地区
                Intent intent=new Intent(AddAdress.this, ProvinceActivity.class);
                startActivity(intent);
                break;
            case R.id.submit:
                System.out.println("------type==="+type);
                if (type.equals("0")){
                    //提交地址信息
                    getsunmit();
                }else {
                    //编辑地址信息
                    geteditadre();
                }

                break;
            }
         }



         public void getsunmit(){
             personName=pename.getText().toString();
             companyName=comname.getText().toString();
             dtaddress=detailtxt.getText().toString();
             moblie=adphone.getText().toString();
             OkHttpUtils.post(BaseUrl.add_adress)
                     .params("userId",uid)
                     .params("provinceId",provinceId)
                     .params("provinceName",provinceName)
                     .params("cityId",cityId)
                     .params("cityName",cityName)
                     .params("countyId",countyId)
                     .params("countyName",countyName)
                     .params("companyName",companyName)
                     .params("personName",personName)
                     .params("address",dtaddress)
                     .params("moblie",moblie)
                     .params("isDefault",isDefault)
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             System.out.println("------------->添加收货地址==="+s);
                             try {
                                 JSONObject js=new JSONObject(s);
                                 boolean State=js.getBoolean("State");
                                 if (State){
                                   finish();
                                 }
                                 String msg=js.getString("Msg");
                                 ToastUtil.showLong(AddAdress.this,msg);
                             } catch (JSONException e) {
                                 e.printStackTrace();
                             }
                         }
                         @Override
                         public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                             super.onError(isFromCache, call, response, e);
                         }
                     });
               }


    public void geteditadre(){
        personName=pename.getText().toString();
        companyName=comname.getText().toString();
        dtaddress=detailtxt.getText().toString();
        moblie=adphone.getText().toString();
        String id=reItem.getId();
        if (provinceId.equals("")){
            provinceId=reItem.getProvinceId();
            provinceName=reItem.getProvinceName();
        }
        if (cityId.equals("")){
            cityId=reItem.getCityId();
            cityName=reItem.getCityName();
        }
        if (countyId.equals("")){
            countyId=reItem.getCountyId();
            countyName=reItem.getCountyName();
          }
        OkHttpUtils.post(BaseUrl.edit_adress)
                .params("id",id)
                .params("provinceId",provinceId)
                .params("provinceName",provinceName)
                .params("cityId",cityId)
                .params("cityName",cityName)
                .params("countyId",countyId)
                .params("countyName",countyName)
                .params("companyName",companyName)
                .params("personName",personName)
                .params("address",dtaddress)
                .params("moblie",moblie)
                .params("isDefault",isDefault)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                finish();
                            }
                            String msg=js.getString("Msg");
                            ToastUtil.showLong(AddAdress.this,msg);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }


}
